package com.zuihai.app.mvp.model;

import android.app.Application;

import com.google.gson.Gson;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;
import com.zuihai.app.mvp.contract.SearchRecyclerContract;
import com.zuihai.app.mvp.model.api.service.SearchService;
import com.zuihai.app.mvp.model.entity.BaseJson;
import com.zuihai.app.mvp.model.entity.FindItem;
import com.zuihai.app.mvp.model.entity.HomeItem;
import com.zuihai.app.mvp.model.entity.MyFootCollectCheckResult;
import com.zuihai.app.mvp.model.entity.SearchHistory;
import com.zuihai.app.mvp.model.entity.SearchHot;

import org.litepal.crud.DataSupport;

import java.util.List;

import javax.inject.Inject;

import rx.Observable;

/**
 * Created by Administrator on 2017/5/6 0006.
 */

@FragmentScope
public class SearchRecyclerModel extends BaseModel implements SearchRecyclerContract.Model {
    private Gson mGson;
    private Application mApplication;
    private SearchService mSearchService;

    @Inject
    public SearchRecyclerModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        mSearchService = repositoryManager.obtainRetrofitService(SearchService.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<BaseJson<SearchHot>> getSearchHot() {
        return mSearchService.searchHot();
    }

    @Override
    public Observable<List<SearchHistory>> getSearchHistory() {
        List<SearchHistory> histories = DataSupport.order("id desc").find(SearchHistory.class);
        if (histories.size()>6){
            histories=histories.subList(0,6);
        }
        return Observable.just(histories);
    }

    @Override
    public Observable<BaseJson<HomeItem>> searchHeadline(String searchStr, String userId, int pageIndex) {
        SearchHistory firstHistory = DataSupport.where("text = ?", searchStr).findFirst(SearchHistory.class);
        if (firstHistory == null){
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }else{
            DataSupport.delete(SearchHistory.class, firstHistory.id);
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }
        return mSearchService.searchHeadline(searchStr, userId, pageIndex);
    }

    @Override
    public Observable<BaseJson<FindItem>> searchSecret(String searchStr, String userId, int pageIndex) {
        SearchHistory firstHistory = DataSupport.where("text = ?", searchStr).findFirst(SearchHistory.class);
        if (firstHistory == null){
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }else{
            DataSupport.delete(SearchHistory.class, firstHistory.id);
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }
        return mSearchService.searchSecret(searchStr, userId, pageIndex);
    }

    @Override
    public Observable<BaseJson<FindItem>> searchCommodity(String searchStr, String userId, int pageIndex) {
        SearchHistory firstHistory = DataSupport.where("text = ?", searchStr).findFirst(SearchHistory.class);
        if (firstHistory == null){
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }else{
            DataSupport.delete(SearchHistory.class, firstHistory.id);
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }
        return mSearchService.searchCommodity(searchStr, userId, pageIndex);
    }

    @Override
    public Observable<BaseJson<FindItem>> searchShop(String searchStr, String userId, int pageIndex) {
        SearchHistory firstHistory = DataSupport.where("text = ?", searchStr).findFirst(SearchHistory.class);
        if (firstHistory == null){
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }else{
            DataSupport.delete(SearchHistory.class, firstHistory.id);
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }
        return mSearchService.searchShop(searchStr, userId, pageIndex);
    }

    @Override
    public Observable<BaseJson<MyFootCollectCheckResult>> searchAll(String searchStr, String userId, int pageIndex) {
        SearchHistory firstHistory = DataSupport.where("text = ?", searchStr).findFirst(SearchHistory.class);
        if (firstHistory == null){
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }else{
            DataSupport.delete(SearchHistory.class, firstHistory.id);
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.text = searchStr;
            searchHistory.save();
        }
        return mSearchService.searchAll(searchStr, userId, pageIndex);
    }
}